home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / edit / ae_14.zip / AE2.PAS < prev    next >
Pascal/Delphi Source File  |  1991-02-27  |  17KB  |  462 lines

  1. unit AE2 ;
  2.  
  3. {$B-}
  4. {$I-}
  5. {$S+}
  6. {$V-}
  7.  
  8. {-----------------------------------------------------------------------------}
  9. { This unit contains all movement procedures.                                 }
  10. { All procedures operate on the current workspace (indicated by CurrentWsnr), }
  11. { unless specified otherwise.                                                 }
  12. {-----------------------------------------------------------------------------}
  13.  
  14. interface
  15.  
  16. uses Crt,Dos,AE0,AE1 ;
  17.  
  18. procedure Home (var P:Position) ;
  19. procedure EndOfLine (var P:Position) ;
  20. procedure CalculateColnr (var P:position) ;
  21. procedure SkipDown (var P:Position ; Distance:word) ;
  22. procedure SkipUp (var P:Position ; Distance:word) ;
  23. procedure WordDown (var P:Position) ;
  24. procedure WordUp (var P:Position) ;
  25. procedure LineDown (var P:Position) ;
  26. procedure LineUp (var P:Position) ;
  27. procedure SearchUp (Target:string ; var P:Position ; LimitIndex:word) ;
  28. procedure SearchDown (Target:string ; var P:Position ; LimitIndex:word) ;
  29. procedure SearchString (Target:string ; var P:Position) ;
  30. procedure MatchBracketsDown (OpenBracket,CloseBracket:char ; var P:Position) ;
  31. procedure MatchBracketsUp (OpenBracket,CloseBracket:char ; var P:Position) ;
  32.  
  33. implementation
  34.  
  35. {-----------------------------------------------------------------------------}
  36. { Sets P to the first column of the line it is pointing to                    }
  37. {-----------------------------------------------------------------------------}
  38.  
  39. procedure Home (var P:Position) ;
  40.  
  41. begin
  42. Dec (P.Index,P.Colnr-1) ;
  43. P.Colnr := 1 ;
  44. end ;
  45.  
  46. {-----------------------------------------------------------------------------}
  47. { Sets P to the last column of the line it is pointing to (CR or end of file) }
  48. {-----------------------------------------------------------------------------}
  49.  
  50. procedure EndOfLine (var P:Position) ;
  51.  
  52. begin
  53. with Workspace[CurrentWsnr] do
  54.      begin
  55.      while (Buffer^[P.Index] <> CR) and
  56.            (P.Index < Buffersize) do
  57.            begin
  58.            Inc (P.Index) ;
  59.            Inc (P.Colnr) ;
  60.            end ;
  61.      end ; { of with }
  62. end ;
  63.  
  64. {-----------------------------------------------------------------------------}
  65. { Re-calculates the column number by searching for a previous line feed       }
  66. {-----------------------------------------------------------------------------}
  67.  
  68. procedure CalculateColnr (var P:position) ;
  69.  
  70. begin
  71. with Workspace[CurrentWsnr] do
  72.      begin
  73.      if P.Linenr = 1
  74.         then P.Colnr := P.Index
  75.         else begin
  76.              P.Colnr := 1 ;
  77.              while (Buffer^[P.Index-P.Colnr] <> CR) do Inc (P.Colnr) ;
  78.              if Buffer^[P.Index-P.Colnr+1] = LF
  79.                 then Dec (P.Colnr) ;
  80.              end ;
  81.      end ;
  82. end ;
  83.  
  84. {-----------------------------------------------------------------------------}
  85. { Skips P <Distance> positions downward, adjusting line and column number.    }
  86. { If the end of the buffer is reached, the procedure stops.                   }
  87. {-----------------------------------------------------------------------------}
  88.  
  89. procedure SkipDown (var P:Position ; Distance:word) ;
  90.  
  91. var Counter : word ;
  92.  
  93. begin
  94. with Workspace[CurrentWsnr] do
  95.      begin
  96.      for Counter := 1 to Distance do
  97.          begin
  98.          if P.Index = BufferSize then Exit ;
  99.          if Buffer^[P.Index] = CR
  100.             then begin
  101.                  Inc (P.Linenr) ;
  102.                  if Buffer^[P.Index+1] = LF
  103.                     then P.Colnr := 0
  104.                     else P.Colnr := 1 ;
  105.                  end
  106.             else Inc (P.Colnr) ;
  107.          Inc (P.Index) ;
  108.          end ;
  109.      end ;
  110. end ;
  111.  
  112. {-----------------------------------------------------------------------------}
  113. { Skips P <Distance> positions upward, adjusting line and column number.      }
  114. { If the start of the buffer is reached, the procedure stops.                 }
  115. {-----------------------------------------------------------------------------}
  116.  
  117. procedure SkipUp (var P:Position ; Distance:word) ;
  118.  
  119. var Counter : word ;
  120.  
  121. begin
  122. with Workspace[CurrentWsnr] do
  123.      begin
  124.      if Distance < P.Colnr
  125.         then begin
  126.              { P will remain within current line }
  127.              Dec (P.Colnr,Distance) ;
  128.              Dec (P.Index,Distance) ;
  129.              end
  130.         else begin
  131.              if P.Index <= Distance
  132.                 then begin
  133.                      { go to start of buffer }
  134.                      P.Index := 1 ;
  135.                      P.Colnr := 1 ;
  136.                      Exit ;
  137.                      end ;
  138.              for Counter := 1 to Distance do
  139.                  begin
  140.                  Dec (P.Index) ;
  141.                  if Buffer^[P.Index] = CR
  142.                     then Dec (P.Linenr) ;
  143.                  end ;
  144.              CalculateColnr (P) ;
  145.              end ;
  146.      end ;
  147. end ;
  148.  
  149. {-----------------------------------------------------------------------------}
  150. { Skips P downward until the beginning of the next word in the text.          }
  151. {-----------------------------------------------------------------------------}
  152.  
  153. procedure WordDown (var P:Position) ;
  154.  
  155. begin
  156. with Workspace[CurrentWsnr] do
  157.      begin
  158.      while not ((Buffer^[P.Index] in WordSeparators) or
  159.                 (P.Index = BufferSize)) do
  160.            begin
  161.            Inc (P.Colnr) ;
  162.            Inc (P.Index) ;
  163.            end ;
  164.      while (Buffer^[P.Index] in WordSeparators) and
  165.            (P.Index < BufferSize) do
  166.            begin
  167.            if Buffer^[P.Index] = CR
  168.               then begin
  169.                    Inc (P.Linenr) ;
  170.                    if Buffer^[P.Index+1] = LF
  171.                     then P.Colnr := 0
  172.                     else P.Colnr := 1 ;
  173.                    end
  174.               else Inc (P.Colnr) ;
  175.            Inc (P.Index) ;
  176.            end ;
  177.      end ;
  178. end ;
  179.  
  180. {-----------------------------------------------------------------------------}
  181. { Skips P upward until the beginning of the previous word in the text.        }
  182. {-----------------------------------------------------------------------------}
  183.  
  184. procedure WordUp (var P:Position) ;
  185.  
  186. begin
  187. with Workspace[CurrentWsnr] do
  188.      begin
  189.      if P.Index > 1
  190.         then begin
  191.              repeat Dec (P.Index) ;
  192.                     if Buffer^[P.Index] = CR
  193.                        then Dec (P.Linenr) ;
  194.              until ((not (Buffer^[P.Index] in WordSeparators)) or
  195.                     (P.Index = 1)) ;
  196.              while ((not (Buffer^[P.Index] in WordSeparators)) and
  197.                     (P.Index > 0)) do
  198.                    begin
  199.                    Dec (P.Index) ;
  200.                    end ;
  201.              Inc (P.Index) ;
  202.              CalculateColnr (P) ;
  203.              end ;
  204.      end ;
  205. end ;
  206.  
  207. {-----------------------------------------------------------------------------}
  208. { Skips P downward to the first column of the next line.                      }
  209. { If the end of the buffer is reached, the procedure stops.                   }
  210. {-----------------------------------------------------------------------------}
  211.  
  212. procedure LineDown (var P:Position) ;
  213.  
  214. var StartIndex : word ;
  215.  
  216. begin
  217. StartIndex := P.Index ;
  218. with Workspace[CurrentWsnr] do
  219.      begin
  220.      while (Buffer^[P.Index] <> CR) and (P.Index < BufferSize) do
  221.            Inc (P.Index) ;
  222.      if (Buffer^[P.Index] = CR)
  223.         then begin
  224.              Inc (P.Index) ;
  225.              if Buffer^[P.Index] = LF
  226.                 then Inc (P.Index) ;
  227.              P.Colnr := 1 ;
  228.              Inc (P.Linenr) ;
  229.              end
  230.         else Inc (P.Colnr,P.Index-StartIndex) ;
  231.      end ;
  232. end ;
  233.  
  234. {-----------------------------------------------------------------------------}
  235. { Skips P upward to the first column of the previous line.                    }
  236. { If the start of the buffer is reached, the procedure stops.                 }
  237. {-----------------------------------------------------------------------------}
  238.  
  239. procedure LineUp (var P:Position) ;
  240.  
  241. begin
  242. if P.Linenr = 1
  243.    then Dec (P.Index,P.Colnr-1)
  244.    else with workspace[CurrentWsnr] do
  245.              begin
  246.              { go past carriage return at star